<?php
if (strpos($_SERVER['PHP_SELF'],basename(__FILE__)) !== false) {
  print "Direct module access forbidden.".basename(__FILE__);
  exit;
}

	global $dbi,$lg,$xview;
	
	// Authority Check
	

	// Tool Bar
	$o_tool = new ToolBar('T1');
	$o_tool->Add(new BTool('iback',"?module=hrmd&action=goals"));
	
	// Retrieve Id Goal if it's edit mode
	$idgoa = $xview->GetValue('idgoa');
	if(!empty($idgoa))
	{
		$sql = "SELECT * FROM go00 WHERE idgoa = $idgoa ";
		$s_data = $dbi->fetch_row($sql,'array');
		if(is_array($s_data ))
		{
			$mode = 'edit';
			// Retrieve Text
			$sql  = "SELECT langu,vtext FROM dd01t ";
			$sql .= "WHERE idelm = 'goanm' AND posnr = '$idgoa' ";
			$t_dd01t = $dbi->fetch_all($sql,'array');
			if(is_array($t_dd01t))
			{
				foreach($t_dd01t as $key => $s_dd01t)
					$t_text[$s_dd01t['langu']] = $s_dd01t['vtext'];
			}
		}
		else
		{
			$xview->redirect('?module=hrmd&action=goals');
			exit;
		}
	}
	else
	{
		$mode = 'new';
		$s_data = $dbi->fetch_all("SHOW COLUMNS FROM go00",'array');
	}
	
	// Get Used Language
	$t_langu = $xview->GetLangu();
	
	// Form
	$o_ecran = new Ecran('E1',"?module=hrmd&action=goals&sact=edit&idgoa=$idgoa&valok=true");
	$o_ecran->Add($o_tool);
	// Master Data
	$o_bloc1 = new Block('B1',$lg->msdat);
	$o_bloc1->Add(new Param('keywd',true,$s_data['keywd']));
	$o_bloc1->Add(new LParam('gotyp',true,true,true,$s_data['gotyp']));
	//$o_bloc1->Add(new CheckBox('goalk',true));
	$o_ecran->Add($o_bloc1);
	// Text Data
	$o_bloc2 = new Block('B2',$lg->mstxt); 
	foreach($t_langu as $key => $value)
	{
		$o_param = new Param('descr',true);
		if(isset($t_text[$key]))
			$o_param->SetDefault($t_text[$key]);

		$o_param->iname = $o_param->idelm.'-'.$key;
		$o_param->ltext .= ' -  '.$value;
		$o_bloc2->Add($o_param);
		unset($o_param);
	}
	$o_ecran->Add($o_bloc2);
	// Save Button
	$o_ecran->Add(new GButton('isave'));
	if(isset($_GET['valok']))
	{
		$t_input = $o_ecran->PGet();
		if(!errors())
		{
			$s_go00 = $o_bloc1->PGet();
			if(empty($idgoa))
			{
				// Insertion 
				$idgoa = $dbi->insert('go00',$s_go00,null,'seq');
				push_msg('I',GMsg('GO',1));
			}
			else
			{
				// Update
				$dbi->update('go00',$s_go00,"WHERE idgoa = '$idgoa' ");
				// Suppression des textes
				$dbi->Delete('dd01t'," idelm = 'goanm' AND posnr = '$idgoa' ");
				push_msg('I',GMsg('GO',2));
			}
			
			if(!empty($idgoa))
			{
				// Insertion des textes
				$t_gtext = $o_bloc2->PGet(); 
				foreach($t_gtext as $key => $value)
				{
					unset($s_dd01t);
					$langu = substr($key,-2);
					$fldnm = substr($key,0,5);
					$s_dd01t['idelm'] = 'goanm';
					$s_dd01t['posnr'] = $idgoa;
					$s_dd01t['langu'] = $langu;
					$s_dd01t['vtext'] = $value;
					$dbi->insert('dd01t',$s_dd01t);	
				}
				$xview->redirect('?module=hrmd&action=goals');
			}
		
		}
	}
	
	echo $o_ecran->Display();
?>